package fem2.jlinalg;

import inf.text.ArrayFormat;
import junit.framework.Assert;

import org.junit.Test;

import fem2.Force;
import fem2.Model;
import fem2.PatchTestT3;
import fem2.analysis.Analysis;
import fem2.analysis.LinearStaticAnalysis;
import fem2.enu.SolverType;

public class UmfPackSolverTest {

	public Model createModel() {
		Model M = new PatchTestT3(0).createModel();

		return M;
	}

	@Test
	public void testSolve1() {
		double F = 0.5;
		Model m = createModel();
		Force l1 = new Force(m.getMesh().getNode(1), F, 0);
		Force l2 = new Force(m.getMesh().getNode(2), F, 0);
		m.addLoad(l1);
		m.addLoad(l2);

		Analysis an = new LinearStaticAnalysis(m);
		an.getStrategy().setSolverType(SolverType.UMFPACK_SOLVER);
		an.run();

		/*
		 * print the displacement and check with theoretical values
		 */
		System.out.println("testSolve1 results:");
		double[][] Uh = new double[][] { { 0, 0 }, { 0.5, 0 }, { 0.5, -0.15 }, { 0, -0.15 } };
		for (int i = 0; i < 4; i++) {
			double[] uHatLocal = m.getMesh().getNode(i).getUHatLocal();
			System.out.println(ArrayFormat.format(uHatLocal));
			Assert.assertEquals(Uh[i][0], uHatLocal[0], 1e-10);
			Assert.assertEquals(Uh[i][1], uHatLocal[1], 1e-10);
		}
	}
}
